package com.lcxw.hxzb.friendcircle.project.system.controller;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.lcxw.hxzb.friendcircle.common.utils.DateUtils;
import com.lcxw.hxzb.friendcircle.common.utils.DistanceUtil;
import com.lcxw.hxzb.friendcircle.common.utils.GetUser;
import com.lcxw.hxzb.friendcircle.framework.web.controller.BaseController;
import com.lcxw.hxzb.friendcircle.framework.web.domain.AjaxResult;
import com.lcxw.hxzb.friendcircle.framework.web.page.TableDataInfo;
import com.lcxw.hxzb.friendcircle.project.system.domain.*;
import com.lcxw.hxzb.friendcircle.project.system.service.IYwpdCommentService;
import com.lcxw.hxzb.friendcircle.project.system.service.IYwpdHistoryService;
import com.lcxw.hxzb.friendcircle.project.system.service.IYwpdLikeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.web.bind.annotation.*;
import com.lcxw.hxzb.friendcircle.project.system.service.IYwpdDynamicService;


/**
 * 用户动态Controller
 *
 * @author jjp
 * @date 2020-07-20
 */
@RestController
@Api("用户动态")
@RequestMapping("/system/dynamic")
public class YwpdDynamicController extends BaseController {

    @Autowired
    private IYwpdDynamicService ywpdDynamicService;

    @Autowired
    private IYwpdHistoryService historyService;

    @Autowired
    private IYwpdLikeService ywpdLikeService;

    /**
     * 查询用户动态列表
     */
    @ApiOperation("查询用户动态列表")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody YwpdDynamic ywpdDynamic) {
        startPage();
        YwpdDynamic newDynamic = new YwpdDynamic();
        newDynamic.setUserId(ywpdDynamic.getUserId());
        newDynamic.setPageNum(ywpdDynamic.getPageNum());
        newDynamic.setPageSize(ywpdDynamic.getPageSize());
        List<YwpdDynamic> list = ywpdDynamicService.selectDynamicList(newDynamic);
        for (int i = 0; i < list.size(); i++) {
            YwpdDynamic dynamic = list.get(i);

            YwpdDynamic ywpdUser = ywpdDynamicService.queryAllUser(dynamic.getUserId());
            dynamic.setUserId(ywpdUser.getUserId());
            dynamic.setImgUrl(ywpdUser.getImgUrl());
            dynamic.setSex(ywpdUser.getSex());
            dynamic.setAge(ywpdUser.getAge());
            dynamic.setNickName(ywpdUser.getNickName());

            String datePoor = DateUtils
                .getDatePoor(DateUtils.getNowDate(), dynamic.getCreateTime());
            dynamic.setTimes(datePoor);
            try {
                dynamic.setDistance(DistanceUtil.getDistance(Double.valueOf(dynamic.getLat()),
                    Double.valueOf(dynamic.getLng()),
                    Double.valueOf(ywpdDynamic.getLat()),
                    Double.valueOf(ywpdDynamic.getLng())));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return getDataTable(list);
    }

    /**
     * 新增用户动态
     */
    @ApiOperation("新增用户动态")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody YwpdDynamic ywpdDynamic) {
        return toAjax(ywpdDynamicService.insertYwpdDynamic(ywpdDynamic));
    }

    /**
     * 修改动态可见范围
     */
    @ApiOperation("修改动态可见范围")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody YwpdDynamic ywpdDynamic) {
        return toAjax(ywpdDynamicService.updateYwpdDynamic(ywpdDynamic));
    }

    /**
     * 删除用户动态
     */
    @ApiOperation("删除用户动态")
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody YwpdDynamic ywpdDynamic) {

        ywpdDynamic.setStatus((long) 1);
        return toAjax(ywpdDynamicService.updateYwpdDynamic(ywpdDynamic));
    }

    /**
     * 用户转发
     */
    @ApiOperation("转发用户动态")
    @PostMapping("/forwardDynamic")
    public AjaxResult forwardDynamic(@RequestBody YwpdDynamic ywpdDynamic) {
        YwpdDynamic dynamic = ywpdDynamicService.selectYwpdDynamicById(ywpdDynamic.getDynamicId());
        dynamic.setUserId(ywpdDynamic.getUserId());
        dynamic.setLat(ywpdDynamic.getLat());
        dynamic.setLng(ywpdDynamic.getLng());
        dynamic.setAddress(ywpdDynamic.getAddress());
        dynamic.setParentContent(ywpdDynamic.getContent());
        dynamic.setParentDynamicId(ywpdDynamic.getDynamicId());
        dynamic.setType(ywpdDynamic.getType());
        int i = ywpdDynamicService.insertYwpdDynamic(dynamic);
        if (i == 1) {
            return AjaxResult.success("转发成功");
        } else {
            return AjaxResult.error("转发失败");
        }
    }


    /**
     * 查看动态详情
     */
    @PostMapping("/getDynamicInfo")
    public AjaxResult getDynamicInfo(@RequestBody YwpdDynamic ywpdDynamic) {
        YwpdDynamic dynamic = ywpdDynamicService.selectYwpdDynamicById(ywpdDynamic.getDynamicId());
        //查询用户信息
        YwpdDynamic ywpdUser = ywpdDynamicService.queryAllUser(dynamic.getUserId());
        dynamic.setImgUrl(ywpdUser.getImgUrl());
        dynamic.setSex(ywpdUser.getSex());
        dynamic.setAge(ywpdUser.getAge());
        dynamic.setNickName(ywpdUser.getNickName());
        String datePoor = DateUtils.getDatePoor(DateUtils.getNowDate(), dynamic.getCreateTime());
        dynamic.setTimes(datePoor);
        try {
            dynamic.setDistance(DistanceUtil.getDistance(Double.valueOf(dynamic.getLat()),
                Double.valueOf(dynamic.getLng()),
                Double.valueOf(ywpdDynamic.getLat()),
                Double.valueOf(ywpdDynamic.getLng())));
        } catch (Exception e) {
            e.printStackTrace();
        }

        YwpdLike ywpdLike = new YwpdLike();
        ywpdLike.setUserId(ywpdDynamic.getUserId());
        ywpdLike.setDynamicId(ywpdDynamic.getDynamicId());
        List<YwpdLike> likes = ywpdLikeService.selectYwpdLikeList(ywpdLike);
        if (likes.isEmpty()) {
            dynamic.setGive(0);
        } else {
            dynamic.setGive(1);
        }

        //新增用户足迹
        YwpdHistory history = new YwpdHistory();
        history.setMemberId(dynamic.getUserId());
        history.setUserId(ywpdDynamic.getUserId());
        history.setType(Long.valueOf(1));
        history.setStatus(Long.valueOf(0));
        historyService.insertYwpdHistory(history);
        //动态查看数+1
        ywpdDynamicService.updateViewsByUser(ywpdDynamic);

        return AjaxResult.success(dynamic);
    }


}
